package com.example.football.service;

import com.example.football.dao.ScheduleDao;
import com.example.football.mapper.PeopleMapper;
import com.example.football.mapper.ScheduleMapper;
import com.example.football.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Service
@Transactional
public class ScheduleServiceImpl implements ScheduleService{
    @Resource
    private ScheduleDao sd;

    @Override
    public List<Schedule> queryAll() {

        return sd.selectAll();
    }

    @Override
    public List<Schedule> queryByTeam(String team) {

        return sd.selectByTeam(team);
    }

    @Override
    public void removeBySchedule(Integer id){
        try {
            sd.deleteBySchedule(id);
        } catch (Exception e) {
            throw new RuntimeException("删除有异常");
        }
    }

    @Override
    public Schedule queryById(Integer id) {

        return sd.selectById(id);
    }

    @Override
    public void addSchedule(Schedule schedule) {
        try {
            sd.insertSchedule(schedule);
        } catch (Exception e) {
            throw new RuntimeException("添加有异常");
        }
    }

    @Override
    public void changeSchedule(Schedule schedule) {
        try {
            sd.updateSchedule(schedule);
        } catch (Exception e) {
            throw new RuntimeException("修改数据异常");
        }
    }

    @Autowired
    private ScheduleMapper scheduleMapper;

    public Result showSchedule(Peopleliketeam peopleid){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            result.setMsg("查询成功");
            result.setSuccess(true);
            List<Schedule> schedule = scheduleMapper.findSchedule(peopleid);
            result.setDetail(schedule);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result showPeopleSchedule(People id){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            result.setMsg("查询成功");
            result.setSuccess(true);
            List<Schedule> schedule = scheduleMapper.findPeopleSchedule(id);
            result.setDetail(schedule);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result findPeopleSchedule(Peopleschedule peopleschedule){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            Integer id = scheduleMapper.findPeoplescheduleid(peopleschedule);
            if(id==null){
                result.setMsg("查询失败");
            }else {
                result.setMsg("查询成功");
                result.setSuccess(true);
                result.setDetail(id);
            }
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result deletePeopschedule(Peopleschedule peopleschedule){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            scheduleMapper.deletePeopleschedule(peopleschedule);
            result.setMsg("删除成功");
            result.setSuccess(true);
            result.setDetail(peopleschedule);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result insertPeopleschedule(Peopleschedule peopleschedule){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            scheduleMapper.insertPeopleschedule(peopleschedule);
            result.setMsg("插入成功");
            result.setSuccess(true);
            result.setDetail(peopleschedule);
        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    public Result showScheduleByleague(Schedule schedule){
        Result result=new Result();
        result.setSuccess(false);
        result.setDetail(null);
        try {
            result.setMsg("查询成功");
            result.setSuccess(true);
            List<Schedule> schedules = scheduleMapper.findScheduleByLeague(schedule);
            result.setDetail(schedules);

        }catch (Exception e){
            result.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
